define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    // add_url: 'order/add',
                    // edit_url: 'order/edit',
                    // del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},

                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'order_status', title: __('Order_status'), searchList: {0:'待支付',1:'进行中',2:'已完成',3:'已失效'}, formatter: Table.api.formatter.normal},

                        {field: 'order_type', title: __('Order_type'), searchList: {"child":__('Child'),"senior":__('Senior'),'RIDER':'陪伴师'}, formatter: Table.api.formatter.normal},
                        {field: 'goodname', title: '商品名', operate: 'LIKE'},
                        {field: 'goodimage', title: '商品图', operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'rider.name', title: __('Riderid')},
                        {field: 'amount', title: __('Amount'), operate:'BETWEEN'},
                        {field: 'coupon_amount', title: __('Coupon_amount'), operate:'BETWEEN'},
                        {field: 'paid_amount', title: __('Paid_amount'), operate:'BETWEEN'},
                       {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'address', title: __('Address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'riderAssign',
                                    text: '指派',
                                    title: '指派',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-flag',
                                    visible:function(row) {
                                        if (row.riderid==null&&row.order_status===1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                        click: function (e, row) {
                                            console.log('riderAssign', e, row);
                                            Controller.api.showRiderDialog(row.id);
                                        }
                                },
                                {
                                    name: 'refund',
                                    text: '退款',
                                    title: '退款',
                                    icon: 'fa fa-list',
                                    extend: 'data-area=\'["40%","50%"]\'',
                                    classname: 'btn btn-xs btn-success btn-magic btn-dialog',
                                    url: 'order/refund?ids={id}',
                                    visible: function (row) {
                                        return (row.is_reach == 0 && row.order_status == 1);
                                    },
                                    click: function (e, row) { // 这里添加 click 事件处理函数
                                        // 打开退款界面
                                        Fast.api.open($(this).data('url'), '退款', {
                                            callback: function (data) {
                                                // 当退款操作完成后执行的操作
                                                if (data.code == 1) {
                                                    Toastr.success(data.msg);
                                                    // 刷新表格
                                                    Controller.api.refresh();
                                                } else {
                                                    Toastr.error(data.msg);
                                                }
                                            }
                                        });
                                    }
                                },
                            ]
                        }

                      ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        refund: function () {
            $(document).ready(function() {
                $('#edit-form').on('submit', function(e) {
                    e.preventDefault(); // 阻止默认表单提交

                if($('#refundReason').val().trim()===''){
                    layer.msg('请填写备注', {
                        icon: 2
                    });
                    return false;
                }
                    // 获取表单数据
                    var formData = $(this).serialize();
                    var ids = new URLSearchParams(formData).get('row[refundId]');

                    // 发送 AJAX 请求
                    $.ajax({
                        url: 'order/refund?ids='+ids, // 这里是你处理退款的 URL
                        method: 'POST',
                        data: formData,
                        success: function(response) {
                            if (response.code === 1) {
                                layer.msg('退款成功');
                                // 退款成功
                                //Toastr.success('退款成功');
                                setTimeout(function() {
                                    Fast.api.close(response);
                                }, 2000);

                                return false;
                            } else {
                                // 退款失败
                                Toastr.error(response.msg || '退款失败');
                            }
                        },
                        error: function(xhr) {
                            // 处理请求错误
                            Toastr.error('系统错误: ' + xhr.statusText);
                        }
                    });
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            // 骑手列表弹窗
            showRiderDialog: function (id) {
                Fast.api.open('companion/online', '在线陪伴师列表', {
                    callback: function (data) {
                        console.log('showRiderDialog callback', data);
                        Controller.api.riderAssign(id,data.row.id);
                    }
                });
            },
            // 指派骑手
            riderAssign: function (order_id, rider_id) {
                var url = 'order/zhipai';
                var data = {
                    id: order_id,
                    riderid: rider_id
                };
                Fast.api.ajax({
                    url: url,
                    data: data,
                    loading: true,
                    success: function (result) {
                        Layer.closeAll();
                        if(result.code==1){
                            Toastr.success('指派成功');

                            // 等待2秒后再刷新页面
                            setTimeout(function() {
                                location.reload();
                            }, 2000);
                        }else{
                            Toastr.error('result.msg');
                        }

                       // location.reload();
                    }
                });
            },
        }
    };


    return Controller;
});
